package cn.stronglink.assetmanage.business.search.model;

import android.graphics.Bitmap;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.BrandDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.RoomDao;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.dao.TypeDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.Type;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.business.search.SearchService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 资产变更数据操作
 * Created by zhengjinglan on 2018/6/20.
 */

public class AssisChangeModel {

    SearchService searchService;
    private Asset syncAsset;
    Room room;

    public onAssisChangeListener assisChangeListener;

    public onAssisChangeListener getAssisChangeListener() {
        return assisChangeListener;
    }

    public void setCheckDetailListener(onAssisChangeListener assisChangeListener) {
        this.assisChangeListener = assisChangeListener;
    }

    public AssisChangeModel() {
        searchService = new SearchService();
    }

    /**
     * 根据资产id获得资产详情
     *
     * @param asset_id
     * @return
     */
    public SearchAsset getAssetById(String asset_id) {
        SearchAsset asset = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder().where(SearchAssetDao.Properties.Id_.eq(asset_id)).
                list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (asset != null) {
            return asset;
        }
        return null;
    }

    /**
     * 更新提交资产数据
     *
     * @param asset
     */
    public void syncAssetData(Asset asset) {
        syncAsset = asset;
        Gson gson = new Gson();
        String checkJson = gson.toJson(syncAsset);
        // 调用服务器接口，同步入库数据
        try {
            searchService.updateAssetData(checkJson, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    if (assisChangeListener != null) {
                        assisChangeListener.onFailure(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (assisChangeListener != null) {
                        assisChangeListener.onResponse(call, response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取品牌列表
     *
     * @return
     */
    public List<Brand> getBrandData() {
        List<Brand> brandList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).
                getBrandDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (brandList != null) {
            return brandList;
        }
        return null;
    }

    /**
     * 获取类型列表
     *
     * @return
     */
    public List<Type> getTypeData() {
        List<Type> typeList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (typeList != null) {
            return typeList;
        }
        return null;
    }

    /**
     * 删除同步的数据
     */
    public void deleteSyncData() {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().delete(syncAsset);
    }

    /**
     * 根据品牌名称获取该品牌的index
     *
     * @param brandName
     * @return
     */
    public List<Brand> getBrandIndex(String brandName) {
        List<Brand> brandList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().
                queryBuilder().where(BrandDao.Properties.Name.eq(brandName)).list();
        if (brandList != null) {
            return brandList;
        }
        return null;
    }

    public Bitmap getBitmapByUrl(String url) {
        return ImageUtils.getIntance().getBitMBitmap(url);
    }

    /**
     * 根据地点名称获取该地点的index
     *
     * @param roomName
     * @return
     */
    public List<Room> getRoomIndex(String roomName) {
        List<Room> roomList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().
                queryBuilder().where(RoomDao.Properties.Name.eq(roomName)).list();
        if (roomList != null) {
            return roomList;
        }
        return null;
    }

    /**
     * 获取地点列表
     *
     * @return
     */
    public List<Room> getRoomData() {
        List<Room> roomList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (roomList != null) {
            return roomList;
        }
        return null;
    }

    /**
     * 根据类型名称获取该类型的index
     *
     * @param typeName
     * @return
     */
    public List<Type> getTypeIndex(String typeName) {
        List<Type> typeList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().
                queryBuilder().where(TypeDao.Properties.Name.eq(typeName)).list();
        if (typeList!=null) {
            return typeList;
        }
        return null;
    }


    /**
     * 更新资产数据
     *
     * @param asset
     */
    public void saveSearchData(Asset asset) {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().update(asset);
    }

    public interface onAssisChangeListener {
        void onFailure(Call call, IOException e);

        void onResponse(Call call, Response response);
    }
}
